/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.guobo.web;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.guobo.entity.Asset;
import com.thinkgem.jeesite.modules.guobo.entity.ExhHall;
import com.thinkgem.jeesite.modules.guobo.entity.NCAsset;
import com.thinkgem.jeesite.modules.guobo.service.AssetService;
import com.thinkgem.jeesite.modules.guobo.service.ExhHallService;
import com.thinkgem.jeesite.modules.sys.utils.JSONSerializer;

/**
 * 资产Controller
 * @author wudavid
 * @version 2017-03-09
 */
@Controller
@RequestMapping(value = "${adminPath}/guobo/asset")
public class AssetController extends BaseController {

	@Autowired
	private AssetService assetService;
	
	@Autowired
	private ExhHallService exhHallService;
	
	@ModelAttribute
	public Asset get(@RequestParam(required=false) String id) {
		Asset entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = assetService.get(id);
		}
		if (entity == null){
			entity = new Asset();
		}
		return entity;
	}
	
	@RequiresPermissions("guobo:asset:view")
	@RequestMapping(value = {"list", ""})
	public String list(Asset asset, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Asset> page = assetService.findPage(new Page<Asset>(request, response), asset); 
		model.addAttribute("page", page);
		return "modules/guobo/assetList";
	}

	@RequiresPermissions("guobo:asset:view")
	@RequestMapping(value = "form")
	public String form(Asset asset, Model model) {
		model.addAttribute("asset", asset);
		return "modules/guobo/assetForm";
	}

	@RequiresPermissions("guobo:asset:edit")
	@RequestMapping(value = "save")
	public String save(Asset asset, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, asset)){
			return form(asset, model);
		}
		asset.setAssetType("0");
		assetService.save(asset);
		addMessage(redirectAttributes, "保存资产成功");
		return "redirect:"+Global.getAdminPath()+"/guobo/asset/list?repage";
	}
	
	@RequiresPermissions("guobo:asset:edit")
	@RequestMapping(value = "delete")
	public String delete(Asset asset,HttpServletRequest request, RedirectAttributes redirectAttributes) {
		String assetType = request.getParameter("assetType");
		assetService.delete(asset);
		addMessage(redirectAttributes, "删除资产成功");
		if(assetType!=null && !"null".equals(assetType) && !"".equals(assetType) && "0".equals(assetType)){
			return "redirect:"+Global.getAdminPath()+"/guobo/asset/list?repage";
		}
		return "redirect:"+Global.getAdminPath()+"/guobo/asset/materialAssetsList?repage";
	}

	@RequiresPermissions("guobo:asset:view")
	@RequestMapping(value = {"findRfidsNotInRole", ""})
	public String findRfidsNotInRole(Asset asset, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Asset> page = assetService.findAllAssets(new Page<Asset>(request, response), asset); 
		//Page<Asset> page = assetService.findRfidsNotInRole(new Page<Asset>(request, response), asset); 
		String warningRoleId = request.getParameter("warningRoleId");
		model.addAttribute("page", page);
		model.addAttribute("warningRoleId", warningRoleId);
		return "modules/guobo/assetsNotInWarningRole";
	}
	
	/**
	 * 获取读写器JSON数据
	 * @param response
	 * @return
	 */
	@RequiresPermissions("guobo:asset:view")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<ExhHall> list = exhHallService.findList(new ExhHall());
		for (int i=0; i<list.size(); i++){
			ExhHall e = list.get(i);
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", e.getId());
			map.put("pId", "0");
			map.put("pIds", "0,");
			map.put("name", e.getName());
			map.put("isParent", true);
			mapList.add(map);
			List<Asset> assets = assetService.findAssetsByExHallId(e.getId());
			for (Asset asset : assets) {
				Map<String, Object> as = Maps.newHashMap();
				as.put("id", asset.getRfid());
				as.put("pId", e.getId());
				as.put("pIds", e.getId());
				as.put("name", asset.getName());
				mapList.add(as);
			}
		}
		return mapList;
	}
	
	/**
	 * 资产位置查询
	 * @param asset
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("guobo:asset:view")
	@RequestMapping(value = {"findAssetExhall", ""})
	public String findAssetExhall(Asset asset, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Asset> page = assetService.findAssetExhall(new Page<Asset>(request, response), asset); 
		model.addAttribute("page", page);
		return "modules/guobo/assetExhall";
	}
	
	/**
     * 分页查询NC资产
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("guobo:asset:view")
    @RequestMapping(value = "searchNCAseets")
    @ResponseBody
    public Map<String,Object> searchNCAseets(HttpServletRequest request,HttpServletResponse response, Model model) {
    	Map<String,Object> data = new HashMap<String,Object>();
    	String page = request.getParameter("page");
    	String pageSize = request.getParameter("pageNum");    	
        try{
        	String name = request.getParameter("name");
        	logger.info("参数name>>>>"+name);
        	logger.info("参数name>>>>"+name);
        	String code = request.getParameter("code");
        	String assetModel = request.getParameter("model");
        	Map<String,Object> param = new HashMap<String,Object>();
        	param.put("name", name);
        	param.put("code", code);
        	param.put("model", assetModel);
        	List<NCAsset> ncAssets = assetService.findNCAssetsByPage(page, pageSize, param);
        	data.put("code", "0");
        	data.put("message", "获取数据成功!");        	;
        	data.put("data", JSONSerializer.serialize(ncAssets));
        	return data;
        }catch(Exception e){
            logger.debug(e.getMessage());
            data.put("code", "1");
        	data.put("message", "获取数据失败!");
        	return data;
        }
    }
    
    @RequiresPermissions("guobo:asset:view")
	@RequestMapping(value = {"materialAssetsList", ""})
	public String materialAssetsList(Asset asset, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Asset> page = assetService.findMaterialAssetsPage(new Page<Asset>(request, response), asset); 
		model.addAttribute("page", page);
		return "modules/guobo/materialAssetList";
	}
    
    @RequiresPermissions("guobo:asset:view")
	@RequestMapping(value = "materialForm")
	public String materialForm(Asset asset, Model model) {
		model.addAttribute("asset", asset);
		return "modules/guobo/materialForm";
	}
    
    /**
     * 分页查询NC资产
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("guobo:asset:view")
    @RequestMapping(value = "searchNCMaterial")
    @ResponseBody
    public Map<String,Object> searchNCMaterial(HttpServletRequest request,HttpServletResponse response, Model model) {
    	Map<String,Object> data = new HashMap<String,Object>();
    	String page = request.getParameter("page");
    	String pageSize = request.getParameter("pageNum");
    	String name = request.getParameter("name");
    	String code = request.getParameter("code");
    	String assetModel = request.getParameter("model");
    	Map<String,Object> param = new HashMap<String,Object>();
    	param.put("name", name);
    	param.put("code", code);
    	param.put("model", assetModel);
        try{
        	List<NCAsset> ncAssets = assetService.findNCMaterialAssetsByPage(page, pageSize, param);
        	data.put("code", "0");
        	data.put("message", "获取数据成功!");        	;
        	data.put("data", JSONSerializer.serialize(ncAssets));
        	return data;
        }catch(Exception e){
            logger.debug(e.getMessage());
            data.put("code", "1");
        	data.put("message", "获取数据失败!");
        	return data;
        }
    }
    
    @RequiresPermissions("guobo:asset:edit")
	@RequestMapping(value = "saveMaterialAsset")
	public String saveMaterialAsset(Asset asset, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, asset)){
			return form(asset, model);
		}
		asset.setAssetType("1");
		assetService.save(asset);
		addMessage(redirectAttributes, "保存非固定资产成功");
		return "redirect:"+Global.getAdminPath()+"/guobo/asset/materialAssetsList?repage";
	}
}